package com.situ.web.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.situ.web.entity.Area;
import com.situ.web.entity.City;
import com.situ.web.entity.Province;
import com.situ.web.service.ISelectAreaService;
import com.situ.web.service.impl.SelectAreaServiceImpl;

import net.sf.json.JSONArray;

public class SelectAreaServlet extends HttpServlet {
	private ISelectAreaService selectAreaService = new SelectAreaServiceImpl();
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String method = req.getParameter("method");
		switch (method) {
		case "selectProvince":
			selectProvince(req,resp);
			break;
		case "selectCity":
			selectCity(req,resp);
			break;
		case "selectArea":
			selectArea(req,resp);
			break;

		default:
			break;
		}
	}

	private void selectArea(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String cityId = req.getParameter("cityId");
		List<Area> list = selectAreaService.selectArea(Integer.parseInt(cityId));
		resp.setContentType("text/html;charset=utf-8");
		JSONArray jsonArray = JSONArray.fromObject(list);
		resp.getWriter().write(jsonArray.toString());
	}

	private void selectCity(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String provinceId = req.getParameter("provinceId");
		List<City> list = selectAreaService.selectCity(Integer.parseInt(provinceId));
		resp.setContentType("text/html;charset=utf-8");
		JSONArray jsonArray = JSONArray.fromObject(list);
		resp.getWriter().write(jsonArray.toString());
	}

	private void selectProvince(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		List<Province> list = selectAreaService.selectProvince();
		resp.setContentType("text/html;charset=utf-8");
		JSONArray jsonArray = JSONArray.fromObject(list);
		resp.getWriter().write(jsonArray.toString());
	}

}
